package com.selfdriving.adapter;

import java.util.List;
import com.selfdriving.R;
import com.selfdriving.activity.PersonInfoActivity;
import com.selfdriving.bean.User;
import com.selfdriving.dialog.MessageDialog;
import com.selfdriving.utils.LogUtil;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class NearbyFriendAdapter extends BaseAdapter
{
	private Context context;

	private List<User> users;

	private HolderView holderView;

	private boolean isEdit = false;

	private OnClickListener clickListener;

	private MessageDialog messageDialog;

	public NearbyFriendAdapter(Context context, List<User> users)
	{
		this.context = context;
		this.users = users;
	}

	@Override
	public int getCount()
	{
		return users.size();
	}

	@Override
	public Object getItem(int poistion)
	{
		return users.get(poistion);
	}

	@Override
	public long getItemId(int poistion)
	{
		return poistion;
	}

	@Override
	public View getView(int poistion, View view, ViewGroup arg2)
	{
		final User user = users.get(poistion);
		if (view == null)
		{
			holderView = new HolderView();
			view = ((LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.nearby_friend_item, null);
			view.setTag(holderView);
		} else
		{
			holderView = (HolderView) view.getTag();
		}
		holderView.icon = (ImageView) view.findViewById(R.id.ivIcon);

		clickListener = new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				LogUtil.e("呵呵");
				switch (v.getId())
				{
				case R.id.dialog_generic_btn_button1:
					messageDialog.dismiss();
					users.remove(user);
					break;
				case R.id.dialog_generic_btn_button2:
					messageDialog.dismiss();
					break;
				default:
					break;
				}
				isEdit = false;
				notifyDataSetChanged();
			}
		};
		holderView.delete = (Button) view.findViewById(R.id.btnDelete);
		holderView.delete.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				messageDialog = new MessageDialog(context, "提示", "是否要退出该结伴游",
						"确定", "我再想想", true, true, clickListener);
				messageDialog.show();
			}
		});
		if (isEdit)
		{
			holderView.delete.setVisibility(View.VISIBLE);
		} else
		{
			holderView.delete.setVisibility(View.GONE);
		}
		holderView.icon.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				Intent intent = new Intent(context, PersonInfoActivity.class);
				context.startActivity(intent);
			}
		});

		return view;
	}

	public void setEdit(boolean isEdit)
	{
		this.isEdit = isEdit;
	}

	public boolean getEdit()
	{
		return isEdit;
	}

	private class HolderView
	{
		private ImageView icon;

		private TextView name;

		private Button delete;

	}

}
